package com.runnor.common.websys.system.util;

import cn.hutool.core.util.IdUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("api/file")
public class FileController {

    @PostMapping("/upload")
    public String uploadFile(@RequestPart("file") MultipartFile file, @Value("${app.upload-base}") String uploadBaseDir) {
        String fileName = file.getOriginalFilename();

        String suffix = fileName.substring(fileName.lastIndexOf("."));

        fileName = IdUtil.simpleUUID() + suffix;

        String filePath = uploadBaseDir + "/" + fileName;

        try {
            file.transferTo(new File(filePath));
        } catch (IOException e) {
            e.getMessage();
        }

        return fileName;
    }

}
